import request from '@/utils/request';
import URLS from './APIUrlDefine';
import {CURRENT_TOKEN} from '@/components/Authorized/renderAuthorize';
import {message} from "antd";

export async function GetTestList(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_TEST_LIST}/${data.current}/${data.pageSize}?id=${data.id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    // console.log(result)
    return Promise.resolve(result);
  });
}

export async function GetFinishedTestList(data: any): Promise<any> {
  return request(`${URLS.GET_FINISHED_EXAM_TEST_LIST}/${data.current}/${data.pageSize}?course=${data.course}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function CreateNewTest(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_EXAM_TEST, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateTest(data: any): Promise<any> {
  return request(URLS.UPDATE_EXAM_TEST, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeTestState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_EXAM_TEST_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteTest(data: any): Promise<any> {
  return request(`${URLS.DELETE_EXAM_TEST}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function GetTestDetail(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_TEST_DETAIL}/${data.id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function SearchGetTestList(keyword: string) {
  return request(`${URLS.SEARCH_EXAM_TEST}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetExamList(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_PAPER_LIST}/${data.current}/${data.pageSize}?course=${data.courseId ? data.courseId : 0}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  }).catch(e => console.error("123123")
  );
}

export async function CreateNewExam(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_EXAM_PAPER, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateExam(data: any): Promise<any> {
  return request(URLS.UPDATE_EXAM_PAPER, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeExamState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_EXAM_PAPER_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteExam(data: any): Promise<any> {
  return request(`${URLS.DELETE_EXAM_PAPER}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function GetExamDetail(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_DETAIL}/${data.id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTrainingList(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_TRAINING_LIST}/${data.current}/${data.pageSize}?id=${data.id ? data.id : -1}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function GetExpList(data: any): Promise<any> {
  return request(`${URLS.GET_EXP_LIST}/${data.current}/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function GetCheckPointerList(data: any): Promise<any> {
  return request(`${URLS.GET_CHECKPOINTER_LIST}/${data.id ? data.id : -1}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function CreateNewCheckPointer(data: any): Promise<any> {
  return request(URLS.ADD_CHECKPOINTER, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateCheckPointer(data: any): Promise<any> {
  return request(URLS.UPDATE_CHECKPOINTER, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteCheckPointer(data: any): Promise<any> {
  return request(`${URLS.DELETE_CHECKPOINTER}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function ChangeCheckPointerState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_CHECKPOINTER_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewTraining(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_EXAM_TRAINING, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateTraining(data: any): Promise<any> {
  return request(URLS.UPDATE_EXAM_TRAINING, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeTrainingState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_EXAM_TRAINING_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteTraining(data: any): Promise<any> {
  return request(`${URLS.DELETE_EXAM_TRAINING}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function GetTrainingDetail(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_TRAINING_DETAIL}/${data.id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function SearchGetExamList(keyword: string) {
  return request(`${URLS.SEARCH_EXAM_PAPER}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTagList(data: any): Promise<any> {
  return request(`${URLS.GET_TAG_LIST}/${data.current}/${data.pageSize}?type=3`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function ChangeTagState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_TAG_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewTag(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_TAG, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateTag(data: any): Promise<any> {
  return request(URLS.UPDATE_TAG, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteTag(data: any): Promise<any> {
  return request(`${URLS.DELETE_TAG}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetTagList(keyword: string) {
  return request(`${URLS.SEARCH_TAG}?key=${keyword}&type=3`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTypeList(data: any): Promise<any> {
  return request(`${URLS.GET_EXAM_TYPE_LIST}/${data.current}/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function ChangeTypeState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_EXAM_TYPE_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewType(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_EXAM_TYPE, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  }).then((resp) => {
      if (resp.code === 10000) {
        return Promise.resolve(resp);
      } else {
        message.error(resp.msg);
        return Promise.reject();
      }
    }
  );
}

export async function UpdateType(data: any): Promise<any> {
  return request(URLS.UPDATE_EXAM_TYPE, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteType(data: any): Promise<any> {
  return request(`${URLS.DELETE_EXAM_TYPE}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetTypeList(keyword: string) {
  return request(`${URLS.SEARCH_EXAM_TYPE}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetQuestionList(data: any): Promise<any> {
  return request(`${URLS.GET_QUESTION_LIST}/${data.current}/${data.pageSize}?type=${data.type}&courseId=${data.courseId}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
    params: {sorter: data.sorter}
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function GetQuestionListByCourseId(data: any): Promise<any> {
  return request(`${URLS.GET_QUESTION_LIST}/${data.current}/${data.pageSize}?type=${data.type}&courseId=${data.courseId}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function CreateNewQuestion(data: any): Promise<any> {

  return request(URLS.CREATE_NEW_QUESTION, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },

    data: JSON.stringify(data)

  });
}

export async function UpdateQuestion(data: any): Promise<any> {
  return request(URLS.UPDATE_QUESTION, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeQuestionState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_QUESTION_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteQuestion(data: any): Promise<any> {
  return request(`${URLS.DELETE_QUESTION}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

//管理员搜索试题
export async function SearchGetQuetionList(data: any): Promise<any> {
  return request(`${URLS.SEARCH_QUESTION}?key=${data.key}&id=${data.id}&type=${data.type}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

//管理员获取试题详情
export async function GetQuestionDetail(data: any): Promise<any> {
  return request(`${URLS.GET_QUESTION_DETAIL}/${data.id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function CreateNewSelection(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_SELECTION, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateSelection(data: any): Promise<any> {
  return request(URLS.UPDATE_SELECTION, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeSelectionState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_SELECTION_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteSelection(data: any): Promise<any> {
  return request(`${URLS.DELETE_SELECTION}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

//添加新的试卷试题
export async function CreateNewPaperQuestion(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_PAPER_QUESTION, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

//添加新的试卷试题测试版本
export async function CreateNewPaperQuestionTest(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_PAPER_QUESTION, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

//更新试卷试题
export async function UpdatePaperQuestion(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_PAPER_QUESTION, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeletePaperQuestion(data: any): Promise<any> {
  return request(`${URLS.DELETE_PAPER_QUESTION}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function GetPaperQuestionList(data: any): Promise<any> {
  return request(`${URLS.GET_PAPER_QUESTION_BY_ZONE}/${data.id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}


//老师批改试卷
export async function ExamResultCorrect(data: any): Promise<any> {
  return request(URLS.POST_EXAM_RESULT_CORRECTING, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}
